
from applications.common.hangingUpCore.operation.base_operation import BaseOperation
# from applications.common.picture_search.recognition_util import RecognitionUtil
import pyautogui as pyautogui


'''
    单击操作
'''
class ClickOperation(BaseOperation):


    '''
        执行操作
    '''
    def execute(self):
        # 封装字典 key - Function
        handleDict = dict(
            coordinate = self.coordinateFunction, # 固定坐标方式
            picture = self.pictureFunction, # 图片特征匹配方式
            absent = self.absentFunction, #不存在默认走absent
        )

        # 处理self.baseParam.position_way默认值 ,如果不存在则走absent
        self.baseParam.position_way = self.baseParam.position_way if self.baseParam.position_way in handleDict else 'absent'
        # 执行
        handleDict[self.baseParam.position_way]()
        pass

    # 坐标方式
    def coordinateFunction(self):
        # 单击
        pyautogui.click(self.baseParam.position_x, self.baseParam.position_y, button='left') 
        self.taskState = True
        pass

    # 图片匹配方式
    def pictureFunction(self):
        # 设置待查询图片和源图片
        self.recognitionUtil.setAttribute(dict(
            queryImageUrl = self.baseParam.picture_url,
            MIN_MATCH_COUNT = self.baseParam.feature_point,
            debugMode =  self.debugMode
        ))
        self.recognitionUtil.setWayOriginImage('screenshot')
        # 执行,获取架监测识别结果
        self.detectionResult = self.recognitionUtil.recognitionCoordinate()
        # 处理执行结果，如果未监测到图片，则直接退出，停止执行
        if self.detectionResult['flag'] :
            self.taskState = True
            return
        # 单击
        pyautogui.click(self.detectionResult['centerPoint'][0], self.detectionResult['centerPoint'][1], button='left') 
        self.taskState = True
        pass


    # 如果key不存在则默认走这个
    def absentFunction(self):
        print('操作方式: key-{}不存在'.format(self.baseParam.position_way))
        pass